﻿using System;
using System.Collections.Generic;
using Dpx.Views;
using Xamarin.Forms;

namespace Dpx.Services {
    /// <summary>
    /// 根页激活服务。
    /// </summary>
    public class RootPageActivationService : IRootPageActivationService {
        // ******** 私有变量

        /// <summary>
        /// 页面键-页面类型字典。
        /// </summary>
        private Dictionary<string, Type> pageKeyTypeDictionary =
            new Dictionary<string, Type> {
                [RootNavigationConstant.QueryPage] = typeof(QueryPage),
                [RootNavigationConstant.TodayPage] = typeof(TodayPage),
                [RootNavigationConstant.InitializationPage] = typeof(InitializationPage),
                [RootNavigationConstant.FavoritePage] = typeof(FavoritePage),
                [RootNavigationConstant.SyncPage] = typeof(SyncPage),

            };

        /// <summary>
        /// 页面缓存。
        /// </summary>
        private Dictionary<string, NavigationPage> cache =
            new Dictionary<string, NavigationPage>();

        // ******** 继承方法

        /// <summary>
        /// 激活页面。
        /// </summary>
        /// <returns>页面键。</returns>
        /// <param name="pageKey">激活的根页面。</param>
        public NavigationPage Activate(string pageKey) =>
            cache.ContainsKey(pageKey)
                ? cache[pageKey]
                : cache[pageKey] = new NavigationPage(
                    (ContentPage) Activator.CreateInstance(
                        pageKeyTypeDictionary[pageKey]));
    }
}